package com.shop.convert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.shop.entity.Product;

public class ProductConverter extends StrutsTypeConverter {

	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {
		//1001,小米手机,1999,2017-01-02
		//1.创建数组
		//Product[] pros  =new Product[values.length];
		List<Product> pros = new ArrayList<Product>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i=0 ;i<values.length; i++) {
			String [] data  =values[i].split(",");
			Product p = new Product();
			p.setId( Integer.parseInt( data[0] )  );
			p.setName(data[1]);
			p.setPrice(Double.parseDouble(data[2]));
			try {
				p.setBuildDate( sdf.parse(data[3]) );
			} catch (ParseException e) {
				e.printStackTrace();
			}
			//pros[i] = p;
			pros.add(p);
		}
		return pros;//返回转换后的数组对象
	}

	@Override
	public String convertToString(Map context, Object o) {
		return o.toString();
	}

}
